package xdTool


import sysAuth.SysUsers
import sysUtil.ResultCode
import sysUtil.ResultJson
import sysUtil.SysAuthSQL
import sysUtil.SysCommUtil
import xdInterface.XdControllerInterface

class XdDataSourceController implements XdControllerInterface{

    def sysDBUtilService
    def authUtilsService
    def index() {
        def result = ResultJson.resultJson()
        def offset = Integer.parseInt(params.page?:1)
        def max = Integer.parseInt(params.limit?:10)
        def userNo = params?.userNo
        def userGrade = authUtilsService.isAvailable(SysUsers.findByUserNo(userNo))
        def sql = SysAuthSQL.getXdDataSourceSQL(userNo,userGrade,params?.searchStr,params?.state)

        def db = sysDBUtilService.getGroovySqlInstance()
        def data = sysDBUtilService.pageResult(sql,offset,max,db)
        def count = sysDBUtilService.pageCount(sql,db)

        result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
        result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
        result.put(ResultJson.DATA.name,data)
        result.put(ResultJson.COUNT.name,count)

        render result
    }


    def update(){
        def result = ResultJson.resultJson()
         try {
             def column = SysCommUtil.underlineToCamel(params.column?:"")

             def value = params.value
             def id = params.id
             def instance =  XdDataSource.get(id)

             instance."${column}" = value
             instance.save(flush: true)
             result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
             result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
             result.put(ResultJson.DATA.name,instance)
         }catch (Exception e){
             result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
             result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
             e.printStackTrace()
         }finally{
             render    result.toString()
        }
    }

    def add(){
        def result = ResultJson.resultJson()
        try {
            def sourceName = params?.sourceName
            def xdDataSource = XdDataSource.findBySourceName(sourceName)
            if(xdDataSource){
                result.put(ResultJson.CODE.name,ResultCode.EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.EXISTS_ERROR.message)
            }else {
                params.creator = params.userNo;
                def a =  new XdDataSource(params)
                a.save(flush: true)
                result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render  result.toString()
        }
    }

    @Override
    def dowLoad() {
        return null
    }

    @Override
    def upLoad() {
        return null
    }

    @Override
    def getOne() {
        return null
    }

    @Override
    def query() {
        index()
    }

    @Override
    def delete() {
        del()
    }

    @Override
    def detail() {
        return null
    }

    def del(){
        def result = ResultJson.resultJson()
        try {
            XdDataSource xdDataSource = XdDataSource.get(params.id)
            xdDataSource.delete(flush: true)
            result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render  result.toString()
        }
    }

    @Override
    def edit() {
        return null
    }

    @Override
    def save() {
        return null
    }
/**
     * 测试链接是否正常
     */
    def test(){
        def result = ResultJson.resultJson()
        try {
            XdDataSource xdDataSource = XdDataSource.get(params.id)
            def db = sysDBUtilService.getGroovySqlInstance(xdDataSource)
            if(db){
                result.put(ResultJson.CODE.name,ResultCode.DB_CONNECT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.DB_CONNECT_SUCCESS.message)
                db = null
            }else {
                result.put(ResultJson.CODE.name,ResultCode.DB_CONNECT_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.DB_CONNECT_ERROR.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.DB_CONNECT_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.DB_CONNECT_ERROR.message)
            e.printStackTrace()
        }finally{
            render  result.toString()
        }
    }

    /**
     * 获取清单，为select使用
     * @return
     */
    def all() {
        def result = ResultJson.resultJson()
        def offset = Integer.parseInt(params.page?:"1")
        def max = Integer.parseInt(params.limit?:"100")
        def userNo = params?.userNo
        def userGrade = authUtilsService.isAvailable(SysUsers.findByUserNo(userNo))
        def sql = SysAuthSQL.getXdDataSourceSQL(null,userGrade,params?.searchStr,1)

        def db = sysDBUtilService.getGroovySqlInstance()
        def data = sysDBUtilService.pageResult(sql,offset,max,db)

        result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
        result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
        result.put(ResultJson.DATA.name,data)
        render result
    }
}
